<?php
namespace Home\Controller;
use Think\Controller;
/**
* 评论
*/
class CommentController extends AdminController
{
	/**
	 * 评论管理
	 * @return [type] [description]
	 */
	public function listComment($order = 'toart')
	{
		$admininfo = $this->checkLogin();
		$this->assign('admininfo', $admininfo);

		$comment = D('Comment');
		$res = $comment
			->table(array('bg_article'=>'a','bg_comment'=>'b'))
			->field('b.id,b.name,b.email,b.toname,a.title,b.comment,b.time')
			->where("a.id = b.toart")
			->order($order)
			->select();
		$this->assign('res', $res);
		$this->display();
	}
	/**
	 * 添加评论
	 */
	public function addComment()
	{
		$comment = D('Comment');
		if (!$comment->create()) {
			alertMes($comment->getError());
		} else {
			$arr['name'] = I('post.name');
			$arr['toart'] = I('post.toart');
			$arr['email'] = I('post.email');
			$arr['comment'] = I('post.comment');
			$arr['time'] = time();
			if (I('post.toname')) {
				$arr['toname'] = I('post.toname');
				$arr['comtId'] = I('post.comtId');
			}
			$res = $comment->add($arr);
			if ($res) {
				alertMes('评论成功', U('index/artdetial/id/'. $arr['toart']));
			} else {
				alertMes('评论失败', U('index/artdetial/id/'. $arr['toart']));
			}
		}
	}
	/**
	 * 删除评论
	 * @param  int $id [description]
	 * @return [type]     [description]
	 */
	public function delComment($id)
	{
		$comment = D('Comment');
		$res = $comment->where('id=' . $id)->delete();
		if ($res) {
			alertMes('删除成功', U('comment/listComment'));
		} else {
			alertMes('删除失败', U('comment/listComment'));
		}
	}
}